<template>
	<!-- 隐患整改下发流程页面 -->
	<view class="workApply-container" :style="{ height: `${windowHeight}px` }">
		<!-- 基础表单校验 -->
		<uni-forms ref="valiForm" :rules="rules" :modelValue="valiFormData" border class="view-form">
			<uni-forms-item label="整改进展情况：" required name="rectificationProgress">
				<uni-easyinput v-model="valiFormData.rectificationProgress" :disabled="rowData.hiddenDangerStatus == 5"
					placeholder="请输入内容"></uni-easyinput>
			</uni-forms-item>

			<uni-forms-item label="实际整改金额(元)：" required name="actualRectificationMoney">
				<uni-easyinput type="number" :disabled="rowData.hiddenDangerStatus == 5"
					v-model="valiFormData.actualRectificationMoney" placeholder="请输入内容"></uni-easyinput>
			</uni-forms-item>

			<uni-forms-item label="防范措施：" required name="precautionaryMeasure">
				<uni-easyinput v-model="valiFormData.precautionaryMeasure" :disabled="rowData.hiddenDangerStatus == 5"
					type="textarea" placeholder="请输入内容"></uni-easyinput>
			</uni-forms-item>

			<uni-forms-item label="存在困难和问题：" required name="haveDifficultProblem">
				<uni-easyinput v-model="valiFormData.haveDifficultProblem" :disabled="rowData.hiddenDangerStatus == 5"
					placeholder="请输入内容"></uni-easyinput>
			</uni-forms-item>

			<template v-if="rowData.hiddenDangerStatus == 5">
		<uni-forms-item label="审批操作：" required name="approveStatus">
				<uni-data-checkbox v-model="valiFormData.approveStatus"
					:localdata="whetherDutyOption"></uni-data-checkbox>
			</uni-forms-item>
			<uni-forms-item label="审批意见：" name="approveRemark">
				<uni-easyinput type="textarea" v-model="valiFormData.approveRemark" focus
					placeholder="请输入内容"></uni-easyinput>
			</uni-forms-item>
			</template>
			<template v-else>
				<uni-forms-item label="整改后文件：" required name="approveStatus">
					<uni-file-picker limit="5" file-mediatype="all" disable-preview @select="select"
						@delete="deleteFile" @success="success"></uni-file-picker>
				</uni-forms-item>
			</template>



		</uni-forms>

		<button type="primary" @click="submit('valiForm')" class="submit-btn">
			确认
		</button>


	</view>
</template>

<script>
	import widowHeightMixins from "@/mixins/widowHeightMixins.js";
	import {
		distributeRect
	} from "@/api/hiddenTrouble/rectification.js";

	export default {
		mixins: [widowHeightMixins],
		components: {

		},
		onLoad(e) {
			// console.log(e, "e");
			const eventChannel = this.getOpenerEventChannel()
			eventChannel.on('acceptDataFromOpenedPage', (data) => {
				this.valiFormData.id = data.checkId
				this.rowData = data.rowData
				// this.valiFormData = data.rowData
				console.log("接受", data)

			});
			// 动态设置标题
			if (this.rowData.hiddenDangerStatus == 4) {

				uni.setNavigationBarTitle({
					title: `隐患整改--反馈`,
				});
			} else if (this.rowData.hiddenDangerStatus == 5) {
				this.valiFormData = this.rowData
				uni.setNavigationBarTitle({
					title: `隐患整改--反馈审批`,
				});
			}

		},
		data() {
			return {
				whetherDutyOption: [{
					"value": 1,
					"text": "通过"
				}, {
					"value": 2,
					"text": "驳回"
				}, ],

				form: {},
				rules: {},
				rowData: {},
				valiFormData: {}
			}
		},
		methods: {
			removeObjectById(arr, id) {
				let index = arr.findIndex(obj => obj.fileId === id);
				if (index !== -1) {
					arr.splice(index, 1);
				}
				return arr;
			},
			//删除上传状态
			deleteFile(e) {
				let file = e.tempFile
				let arr = this.removeObjectById(this.fileInfoList, file.uuid)


			},
			// 获取上传状态
			select(e) {
				console.log('选择文件：', e)
				let file = e.tempFiles[0]

				this.fileInfoList.push({
					fileId: file.uuid,
					fileName: file.name,
					type: 61,
					url: file.url
				})
				// console.log("新增数组的", this.fileInfoList)
			},


			// 上传成功
			success(e) {
				console.log('上传成功', e)
			},
			//提交
			submit() {
				this.$refs.valiForm.validate().then(res => {
					// console.log('表单数据信息：', res);
					this.valiFormData.id = this.rowData.id;
					// console.log("tijaio", this.valiFormDatas)
					distributeRect(this.valiFormData).then((res) => {
						console.log("res==", res)
						uni.showToast({
							title: '审批成功',
							duration: 2000
						})
						// uni.navigateBack()
						uni.navigateTo({
							url: "/pages/trouble/index",
							success: (res) => {
								res.eventChannel.emit('acceptDataFromOpenerPage', {
									page: 'manage',
									title: '计划反馈'
								})
							}
						})
					})
				}).catch(err => {
					console.log('表单错误信息：', err);
				})

			}
		}
	}
</script>

<style lang="scss" scoped>
	.workApply-container {
		padding: 20rpx;
		background-color: #fff;
		overflow-y: auto;

		.view-form {}

		::v-deep {
			.uni-easyinput {
				border: 1px solid #e5e5e5;

				.is-disabled {
					color: #646464;
				}
			}

			.uni-forms-item--border {
				border-top: 0 solid #000;
				border-bottom: 2rpx solid #e8eaec;
			}

			.uni-forms-item__label {
				width: 40% !important;
			}

			.submit-btn {
				margin-top: 30rpx;
				background: #137ee9;
				border-radius: 10rpx;
			}
		}
	}
</style>